﻿CREATE PROCEDURE [dbo].[yaf_forum_listallmymoderated](
                @BoardID INT,
                @UserID  INT)
AS
    BEGIN
        SELECT   b.CategoryID,
                 Category = b.Name,
                 a.ForumID,
                 Forum = a.Name,
                 x.Indent
        FROM     (SELECT b.ForumID,
                         Indent = 0
                  FROM   yaf_Category a
                         JOIN yaf_Forum b
                           ON b.CategoryID = a.CategoryID
                  WHERE  a.BoardID = @BoardID
                  AND b.ParentID IS NULL
                  UNION 
                  SELECT c.ForumID,
                         Indent = 1
                  FROM   yaf_Category a
                         JOIN yaf_Forum b
                           ON b.CategoryID = a.CategoryID
                         JOIN yaf_Forum c
                           ON c.ParentID = b.ForumID
                  WHERE  a.BoardID = @BoardID
                  AND b.ParentID IS NULL
                  UNION 
                  SELECT d.ForumID,
                         Indent = 2
                  FROM   yaf_Category a
                         JOIN yaf_Forum b
                           ON b.CategoryID = a.CategoryID
                         JOIN yaf_Forum c
                           ON c.ParentID = b.ForumID
                         JOIN yaf_Forum d
                           ON d.ParentID = c.ForumID
                  WHERE  a.BoardID = @BoardID
                  AND b.ParentID IS NULL) AS x
                 JOIN yaf_Forum a
                   ON a.ForumID = x.ForumID
                 JOIN yaf_Category b
                   ON b.CategoryID = a.CategoryID
                 JOIN yaf_vaccess c
                   ON c.ForumID = a.ForumID
        WHERE    c.UserID = @UserID
        AND b.BoardID = @BoardID
        AND c.ModeratorAccess > 0
        ORDER BY b.SortOrder,
                 a.SortOrder
    END


